﻿using System.Security.Cryptography;


namespace HEFramework
{
    /// <summary>
    ///
    /// Rijndael数据加密类
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2022-6-29 10:56:06
    /// ----------------------------------------
    /// </summary>
    public class RijndaelAes
    {
        private static RijndaelManaged managed = new();

        public RijndaelAes()
        {
            managed.Mode = CipherMode.CBC;
            managed.Padding = PaddingMode.Zeros;
            managed.GenerateKey();
            managed.GenerateIV();
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="_bytes"></param>
        /// <returns></returns>
        public byte[] Encrypt(byte[] _bytes)
        {
            ICryptoTransform encryptor = managed.CreateEncryptor();
            return encryptor.TransformFinalBlock(_bytes, 0, _bytes.Length);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="_bytes"></param>
        /// <returns></returns>
        public byte[] Decrypt(byte[] _bytes)
        {
            ICryptoTransform transform = managed.CreateDecryptor();
            return transform.TransformFinalBlock(_bytes, 0, _bytes.Length);
        }
    }
}